#pragma strict
//Game Objects
var ambient : AudioClip;

private var scriptsGObj : GameObject;
private var mainCameraGobj : GameObject;
private var fpcGObj : GameObject;

//Components
private var guiSurface : GUISurface;
private var player : Player;

//GUI related
private var defaultSkin : GUISkin; 
private var GUIEnabled : boolean = false;

//Screen stuff
private var w2 : int;
private var h2 : int;
private var zoomLevel : int = 0;
private var resolutions : Resolution[];
private var buttonSizeX = 80;
private var buttonSizeY = 30;

private var oldPlayerScore : int = 0;

//Sounds
private var ambientAudioSource : AudioSource;

function Awake (){
	//Screen and resolution
	w2 = Screen.width/2;
	h2 = Screen.height/2;
	resolutions = Screen.resolutions;
	Screen.lockCursor = true;  //Never leave the view
	Screen.showCursor = false; //Dont show curser
	
	//TODO outsource do it better
	ambientAudioSource = gameObject.AddComponent(AudioSource);
	ambientAudioSource.clip = ambient;
	ambientAudioSource.loop = true;
	ambientAudioSource.volume = 3;
	ambientAudioSource.playOnAwake = false;
}

function Start () {
	scriptsGObj = GameObject.FindGameObjectWithTag("Scripts"); //Finds the GameObject with Tag Scripts (to get the Component)
	mainCameraGobj = GameObject.FindGameObjectWithTag("MainCamera");
	fpcGObj = GameObject.FindGameObjectWithTag("FPC");
	
	ResetPlayerStats(); //!!! Need to be called after scriptsGObj = GameObject..... !!!
	player = scriptsGObj.GetComponent(Player);
	guiSurface = scriptsGObj.GetComponent(GUISurface); 
	
	ambientAudioSource.Play();
}

function Update () {
	if(Input.GetKeyUp("f10")){
		GUIEnabled = !GUIEnabled;
	}
	//Zoom on right mouse button
	if(Input.GetKeyDown(KeyCode.Mouse1)){
		if(zoomLevel >= 2){
			mainCameraGobj.camera.fov = 60;
			zoomLevel = 0;
		}else{
			mainCameraGobj.camera.fov = mainCameraGobj.camera.fov/2;
			zoomLevel++;
		}
	}
	
	Screen.lockCursor = !GUIEnabled;
	Screen.showCursor = GUIEnabled;
	
	GameManagement(); //TODO outsource !?!
}

function OnGUI() {
	GUI.skin = defaultSkin;
	GUI.color = Color.green;
	GUI.backgroundColor = Color.green;	
	
	if(!GUIEnabled){
		guiSurface.createHUDGUI();
		fpcGObj.GetComponent(MouseLook).sensitivityX = 1.5;
		fpcGObj.GetComponent(MouseLook).sensitivityY = 1.5;
		Time.timeScale = 1;	//activate the game
	}
	if (GUIEnabled) {
		fpcGObj.GetComponent(MouseLook).sensitivityX = 0;
		fpcGObj.GetComponent(MouseLook).sensitivityY = 0;
		Time.timeScale = 0; //paus the game
		guiSurface.createMenuGUI();
	}
}

public function ResetPlayerStats(){
	//Paththrou value to player script on GameObject(scripts)
	scriptsGObj.SendMessage("setPlayerLifes", 3);
	scriptsGObj.SendMessage("setPlayerDamage", 0);
}

public function GameManagement(){
	//TODO better difficulty/level management
	if(player.getPlayerScore() - oldPlayerScore == 6){
		oldPlayerScore = player.getPlayerScore();
		scriptsGObj.GetComponent(BuildTank).increaseTankAmount(1); //first tank spawns directly on start
		scriptsGObj.GetComponent(BuilObstacle).decreaseObstacleAmount(1);
	}
	
	if(player.getPlayerLifes() <= 0 && scriptsGObj.GetComponent(GUISurface).getSaveMenueActive() == false){
		scriptsGObj.SendMessage("setSaveMenueActive");
		GUIEnabled = true;
	}
}

//Setter and Getter
public function isGUIEnabled() : boolean{
	return GUIEnabled;
}

public function setGUIEnabled(value : boolean){
	GUIEnabled = value;
}